package com.seanbot.armstrong.passcodestoragerc4;

import java.io.UnsupportedEncodingException;

import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class PassCodeRC4 extends Activity {

	SharedPreferences sp;
	EditText etPW;
	EditText etKey;
	EditText etVal;
	EditText etEnc;
	CipherAlgorithm ca = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pass_code_rc4);

		etPW = (EditText) findViewById(R.id.editTextPassword);
		etKey = (EditText) findViewById(R.id.editTextKey);
		etVal = (EditText) findViewById(R.id.editTextValue);
		etEnc = (EditText) findViewById(R.id.editTextEncrypted);
		sp = this.getSharedPreferences("mydata", this.MODE_PRIVATE);

		Button buttonPut = (Button) findViewById(R.id.buttonPut);
		Button buttonGet = (Button) findViewById(R.id.buttonGet);

		etPW.addTextChangedListener(new TextWatcher() {

			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}

			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				etPW = (EditText) findViewById(R.id.editTextPassword);
				if (etPW.length() > 0) {
					String keyStr = etPW.getText().toString();

					try {
						createNewCipherAlg(keyStr);
						ca.createStream();
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
					}

				}
			}

		});

		buttonPut.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				String key = etKey.getText().toString();
				String val = etVal.getText().toString();
				String enc = "";
				
				if(key.length() > 0){

				try {
					ca.createStream();
					enc = ca.encrypt(val);
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}

				etEnc.setText(enc);
				SharedPreferences.Editor ed = sp.edit();
				ed.putString(key, enc);
				ed.apply();
				} else {
					etEnc.setText("<<ERROR: Key must be at least 1 character.>>");
				}
			}
		});

		buttonGet.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				String key = etKey.getText().toString();
				String val;
				String decrypted = "";
				etVal.setText("");

				if (sp.contains(key) && key.length() > 0) {
					val = sp.getString(key, "<empty>");
					try {
						ca.createStream();
						decrypted = ca.decrypt(val);
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					etEnc.setText(val);
					etVal.setText(decrypted);
				} else {
					etEnc.setText("<<ERROR: Not a stored key.>>");
				}
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_pass_code_rc4, menu);
		return true;
	}

	public void createNewCipherAlg(String key)
			throws UnsupportedEncodingException {
		ca = new CipherAlgorithm(key);
	}
}
